package com.inossem.warehousestorage_inossem.activity.up_frame;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.inossem.loadandrefresh.pullableview.PullableListView;
import com.inossem.utils.base.QRCodeUtils;
import com.inossem.utils.base.Utils;
import com.inossem.utils.constant.QRCodeConstant;
import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.base.BaseActivity;
import com.inossem.warehousestorage_inossem.activity.up_frame.extra.UpFrameExtra;
import com.inossem.warehousestorage_inossem.adapter.up_frame.UpFrameOverviewAdapter;
import com.inossem.warehousestorage_inossem.bean.up_frame.UpFrameOverviewBean;
import com.inossem.warehousestorage_inossem.dialog.DialogUtils;
import com.inossem.warehousestorage_inossem.listener.GsonBeanListener;
import com.inossem.warehousestorage_inossem.request.up_frame.UpFrameOverviewRequest;
import com.inossem.warehousestorage_inossem.utils.ActivityUtils;
import com.inossem.warehousestorage_inossem.utils.SPUtils;
import com.inossem.warehousestorage_inossem.utils.UpFrameUtils;

import java.util.List;

/**
 * Created by Administrator on 2017/7/8 0008.
 */

public class UpFrameOverviewActivity extends BaseActivity {

    private TextView title, nextBtn;

    private PullableListView upFrameList;

    private UpFrameOverviewAdapter adapter;

    private String requestNumberStr, mMaterielCode;

    private List<UpFrameOverviewBean.MaterialListBean.ItemListBean> allData;

    private void getExtra(Activity activity) {
        requestNumberStr = activity.getIntent().getStringExtra(UpFrameExtra.REQUEST_NUMBER);
    }

    @Override
    protected void onCreateImpl(Bundle savedInstanceState) {
        //清空sp的上架信息
        SPUtils.removeUpFrameInformation(UpFrameOverviewActivity.this);
        setContentView(R.layout.activity_up_frame_overview);
        initView(UpFrameOverviewActivity.this);
    }

    @Override
    protected void onResumeImpl() {
        initData(UpFrameOverviewActivity.this);
    }

    private void initData(UpFrameOverviewActivity activity) {
        title.setText(activity.getResources().getString(R.string.activity_up_frame_overview_title));
        ActivityUtils.initBackAndFunction(activity, R.string.activity_up_frame_overview_back, new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                DialogUtils.getConfirmDialog(UpFrameOverviewActivity.this, getResources().getString(R.string.activity_up_frame_overview_fill_out_the_information), new DialogUtils.ConfirmDialogListener() {
                    @Override
                    public void onConfirm(Dialog dialog) {
                        UpFrameOverviewActivity.super.onBackPressed();
                    }
                }).show();
            }
        });
        upFrameList.setPullUpFlag(false);
        upFrameList.setPullDownFlag(false);
        nextBtn.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                if (UpFrameUtils.getUpFrameConfirmNumber(activity) == 0) {
                    Utils.middleToast(activity, getResources().getString(R.string.activity_up_frame_overview_commit_prompt));
                } else {
                    Intent intent = new Intent(activity, UpFrameCommitActivity.class);
                    activity.startActivity(intent);
                }
            }
        });
        getExtra(activity);
        getData(activity);
        setmScanListener((String scanResult, int length) -> {
            mMaterielCode = QRCodeUtils.DecryptQRCode(scanResult);
            getData(activity);
        }, QRCodeConstant.TYPE_MATERIEL_CODE);
    }

    private void getData(Context activity) {
        UpFrameOverviewBean bean = SPUtils.getUpFrameInformation(activity);
        if (bean == null) {
            UpFrameOverviewRequest.request(activity, requestNumberStr, new GsonBeanListener<UpFrameOverviewBean>() {
                @Override
                public void onSuccess(UpFrameOverviewBean bean) {
                    allData = bean.getBody().getItem_list();
                    SPUtils.saveUpFrameInformation(activity, bean);
                    addUpFrameListData(activity, bean.getBody(), ActivityUtils.getShowData(allData, 0), upFrameList, true);
                }
            });
        } else {
            if (TextUtils.isEmpty(mMaterielCode)) {
                allData = bean.getBody().getItem_list();
            } else {
                allData = UpFrameUtils.getUpFrameMaterielList(activity, mMaterielCode);
            }
            addUpFrameListData(activity, bean.getBody(), ActivityUtils.getShowData(allData, 0), upFrameList, true);
        }
    }

    private void addUpFrameListData(Context context, UpFrameOverviewBean.MaterialListBean bean, List<UpFrameOverviewBean.MaterialListBean.ItemListBean> upFrameItemList, ListView listView, boolean flag) {
        if (flag) {
            adapter = new UpFrameOverviewAdapter(context, bean, upFrameItemList, mMaterielCode, (String materielCode) -> {
                mMaterielCode = materielCode;
                getData(context);
            });
            listView.setAdapter(adapter);
        }
    }

    private void initView(UpFrameOverviewActivity activity) {
        title = (TextView) activity.findViewById(R.id.headerTitle);
        upFrameList = (PullableListView) activity.findViewById(R.id.upFrameList);
        nextBtn = (TextView) activity.findViewById(R.id.nextBtn);
    }

    @Override
    public void onBackPressed() {
        DialogUtils.getConfirmDialog(this, getResources().getString(R.string.activity_up_frame_overview_fill_out_the_information), new DialogUtils.ConfirmDialogListener() {
            @Override
            public void onConfirm(Dialog dialog) {
                UpFrameOverviewActivity.super.onBackPressed();
            }
        }).show();
    }

    public interface UpFrameOverviewListener {
        void onNoDoubleClickListener(String materielCode);
    }

}
